﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using BCCommon;
using BCEntity.MakeStaticPagingComponent.SystemServiceListing;
using Dapper;

namespace BCData.MakeStaticPagingComponent.SystemServiceListing
{
    /// <summary>
    /// 系统服务接口数据处理
    /// </summary>
    public class SystemServiceListingData : ISystemServiceListingData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY = @"SELECT SystemServiceListingId,WebsiteTemplateId,DisplayName,Description,ServiceName,MethodName,ParamsText,ParamsDesc,
                                              ResultType,ResultEntityName,CreateTime,UpdateTime,IsSystem
                                        FROM SystemServiceListing";
        public SystemServiceListingData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }

        public SystemServiceListingEntity Add(SystemServiceListingEntity systemServiceListingEntity)
        {
            string sql = @"INSERT INTO SystemServiceListing(WebsiteTemplateId,DisplayName,Description,ServiceName,MethodName,ParamsText,ParamsDesc,ResultType,ResultEntityName)
                                        VALUES(?WebsiteTemplateId,?DisplayName,?Description,?ServiceName,?MethodName,?ParamsText,?ParamsDesc,?ResultType,?ResultEntityName);";
            sql = string.Format("{0} {1} WHERE SystemServiceListingId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteTemplateId", systemServiceListingEntity.WebsiteTemplateId, DbType.Int32, size: 11);
            parameters.Add("?DisplayName", systemServiceListingEntity.DisplayName, DbType.AnsiString, size: 30);
            parameters.Add("?Description", systemServiceListingEntity.Description, DbType.AnsiString, size: 500);
            parameters.Add("?ServiceName", systemServiceListingEntity.ServiceName, DbType.AnsiString, size: 100);
            parameters.Add("?MethodName", systemServiceListingEntity.MethodName, DbType.AnsiString, size: 100);
            parameters.Add("?ParamsText", systemServiceListingEntity.ParamsText, DbType.AnsiString, size: 1000);
            parameters.Add("?ParamsDesc", systemServiceListingEntity.ParamsDesc, DbType.AnsiString, size: 1000);
            parameters.Add("?ResultType", systemServiceListingEntity.ResultType.ToString(), DbType.AnsiString);
            parameters.Add("?ResultEntityName", systemServiceListingEntity.ResultEntityName, DbType.AnsiString, size: 500);
            return this.databaseContext.Master.QueryEntity<SystemServiceListingEntity>(sql, parameters, databaseContext.Transaction);
        }

        public bool CheckSystemServiceListingExists(int websiteTemplateId,string displayName)
        {
            string sql = @"SELECT 1 FROM SystemServiceListing WHERE WebsiteTemplateId=?WebsiteTemplateId AND DisplayName=?DisplayName LIMIT 1;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteTemplateId", websiteTemplateId, DbType.Int32, size: 11);
            parameters.Add("?DisplayName", displayName, DbType.AnsiString, size: 30);
            object result = this.databaseContext.Slave.ExecuteScalar(sql, parameters);
            return result != null;
        }

        public bool CheckSystemServiceListingExists(int systemServiceListingId)
        {
            string sql = @"SELECT 1 FROM SystemServiceListing WHERE SystemServiceListingId=?SystemServiceListingId LIMIT 1;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SystemServiceListingId", systemServiceListingId, DbType.Int32, size: 11);
            object result = this.databaseContext.Slave.ExecuteScalar(sql, parameters);
            return result != null;
        }

        public void Delete(int systemServiceListingId)
        {
            string sql = @"DELETE FROM SystemServiceListing WHERE SystemServiceListingId=?SystemServiceListingId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SystemServiceListingId", systemServiceListingId, DbType.Int32, size: 11);
            this.databaseContext.Master.Execute(sql, parameters, databaseContext.Transaction);
        }

        public SystemServiceListingEntity Get(int systemServiceListingId)
        {
            string sql = string.Format("{0} WHERE SystemServiceListingId=?SystemServiceListingId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SystemServiceListingId", systemServiceListingId, DbType.Int32, size: 11);
            return this.databaseContext.Slave.QueryEntity<SystemServiceListingEntity>(sql, parameters);
        }

        public SystemServiceListingEntity Get(int websiteTemplateId, string methodName)
        {
            string sql = string.Format("{0} WHERE WebsiteTemplateId=?WebsiteTemplateId AND MethodName=?MethodName", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteTemplateId", websiteTemplateId, DbType.Int32, size: 11);
            parameters.Add("?MethodName", methodName, DbType.AnsiString, size: 100);
            return this.databaseContext.Slave.QueryEntity<SystemServiceListingEntity>(sql, parameters);
        }

        public SystemServiceListingEntity Get(string methodName)
        {
            string sql = string.Format("{0} WHERE WebsiteTemplateId=(SELECT WebsiteTemplateId FROM WebsiteTemplate WHERE IsUsed=1 LIMIT 1) AND MethodName=?MethodName", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?MethodName", methodName, DbType.AnsiString, size: 100);
            return this.databaseContext.Slave.QueryEntity<SystemServiceListingEntity>(sql, parameters);
        }

        public IEnumerable<SystemServiceListingEntity> Query(int websiteTemplateId)
        {
            string sql = string.Format(@"{0} WHERE WebsiteTemplateId=?WebsiteTemplateId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteTemplateId", websiteTemplateId, DbType.Int32, size: 11);
            return this.databaseContext.Slave.Query<SystemServiceListingEntity>(sql, parameters);
        }

        public IEnumerable<SystemServiceListingEntity> Query(int websiteTemplateId,string displayName, out int dataCount, bool? ascending, int index = 1, int size = 20)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            wheres.Add("WebsiteTemplateId=?WebsiteTemplateId");
            parameters.Add("?WebsiteTemplateId", websiteTemplateId, DbType.Int32, size: 11);
            if (!string.IsNullOrEmpty(displayName))
            {
                wheres.Add("DisplayName=?DisplayName");
                parameters.Add("?DisplayName", displayName, DbType.AnsiString, size: 30);
            }
            
            string sql = string.Format(@"{0} {1}; SELECT Count(1) FROM SystemServiceListing {1}",
                 QUERY,
                 wheres.Count > 0 ? "WHERE " + string.Join(" AND ", wheres) : string.Empty,
                 (index == 0 ? 0 : index - 1) * size,
                 size,
                 ascending.HasValue && ascending.Value ? "ASC" : "DESC"
            );
            var result = this.databaseContext.Slave.QueryMultiple<SystemServiceListingEntity, int>(sql, parameters);

            dataCount = result.Item2;

            return result.Item1;
        }

        public SystemServiceListingEntity Update(SystemServiceListingEntity systemServiceListingEntity)
        {
            string sql = @"UPDATE SystemServiceListing SET
                                DisplayName=?DisplayName,
                                Description=?Description,
                                ServiceName=?ServiceName,
                                MethodName=?MethodName,
                                ParamsText=?ParamsText,
                                ParamsDesc=?ParamsDesc,
                                ResultType=?ResultType,
                                ResultEntityName=?ResultEntityName
                            WHERE SystemServiceListingId=?SystemServiceListingId;";
            sql = string.Format("{0} {1} WHERE SystemServiceListingId=?SystemServiceListingId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SystemServiceListingId", systemServiceListingEntity.SystemServiceListingId, DbType.Int32, size: 11);
            parameters.Add("?DisplayName", systemServiceListingEntity.DisplayName, DbType.AnsiString, size: 30);
            parameters.Add("?Description", systemServiceListingEntity.Description, DbType.AnsiString, size: 500);
            parameters.Add("?ServiceName", systemServiceListingEntity.ServiceName, DbType.AnsiString, size: 100);
            parameters.Add("?MethodName", systemServiceListingEntity.MethodName, DbType.AnsiString, size: 100);
            parameters.Add("?ParamsText", systemServiceListingEntity.ParamsText, DbType.AnsiString, size: 1000);
            parameters.Add("?ParamsDesc", systemServiceListingEntity.ParamsDesc, DbType.AnsiString, size: 1000);
            parameters.Add("?ResultType", systemServiceListingEntity.ResultType.ToString(), DbType.AnsiString);
            parameters.Add("?ResultEntityName", systemServiceListingEntity.ResultEntityName, DbType.AnsiString, size: 500);
            return this.databaseContext.Master.QueryEntity<SystemServiceListingEntity>(sql, parameters, databaseContext.Transaction);
        }

    }
}
